package com.hskn.hss.module.button.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hskn.hss.core.annotation.SysLog;
import com.hskn.hss.core.controller.BaseController;
import com.hskn.hss.core.enums.BusinessType;
import com.hskn.hss.entity.AjaxResult;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.alarmsheshi.entity.AlarmSheshi;
import com.hskn.hss.module.button.entity.SysButton;
import com.hskn.hss.module.button.mapper.SysButtonMapper;
import com.hskn.hss.module.button.service.ISysButtonService;
import com.hskn.hss.module.menu.entity.SysMenu;
import com.hskn.hss.module.menu.service.ISysMenuService;
import com.hskn.hss.module.tlequip.mapper.TlEquipMapper;
import com.hskn.hss.utils.StringUtils;
import com.hskn.hss.utils.Tools;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hskn
 * @since 2021-05-04
 */
@Slf4j
@RestController
@RequestMapping("/hss/sysbutton")
public class SysButtonController extends BaseController {

    @Autowired
    private ISysButtonService sysButtonService;
    @Autowired
    private ISysMenuService sysMenuService;

    @SysLog(businessType = BusinessType.INSERT, tableName = "sys_button", bean = SysButton.class, beanMapper = SysButtonMapper.class, names = "name")
    @RequestMapping(value = "insertButton", method = RequestMethod.POST)
    public AjaxResult<Object> insertButton(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            return sysButtonService.insert(params);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @SysLog(businessType = BusinessType.UPDATE, tableName = "sys_button", idKey = "id", bean = SysButton.class, beanMapper = SysButtonMapper.class, names = "name")
    @RequestMapping(value = "updateButton", method = RequestMethod.POST)
    public AjaxResult<Object> updateButton(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            return sysButtonService.update(params);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @SysLog(businessType = BusinessType.DELETE, tableName = "sys_button", idKey = "id", bean = SysButton.class, beanMapper = SysButtonMapper.class, names = "name")
    @RequestMapping(value = "deleteButtonById", method = RequestMethod.POST)
    public AjaxResult<Object> deleteButtonById(HttpServletRequest request) {
        JSONObject params = JSON.parseObject(Tools.getBody(request));
        try {
            return sysButtonService.deleteByid(params);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("删除失败，请稍后再试");
        }
    }

    @RequestMapping(value = "getButtonById", method = RequestMethod.POST)
    public AjaxResult<Object> getButtonById(HttpServletRequest request) {
        JSONObject params = JSON.parseObject(Tools.getBody(request));
        SysButton sysButton = null;
        try {
            sysButton = sysButtonService.getInfoById(params);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("获取信息失败，请稍后再试");
        }
        return AjaxResult.success(sysButton);

    }

    @RequestMapping(value = "getButtonList", method = RequestMethod.POST)
    public AjaxResult<Object> getButtonList(HttpServletRequest request) {
        JSONObject params = JSON.parseObject(Tools.getBody(request));
        try {
            JSONObject json = sysButtonService.getList(params);
            return AjaxResult.success(json);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("获取信息失败，请稍后再试");
        }
    }
    @PostMapping(value = "generateButtonData")
    public AjaxResult generateButtonData() {
        List<SysButton> sysButtonList = sysButtonService.selectList(new QueryWrapper<>());
        if (CollectionUtils.isNotEmpty(sysButtonList)) {
            sysButtonService.remove(new QueryWrapper<SysButton>()
                    .lambda().in(SysButton::getId,sysButtonList.stream().map(SysButton::getId).collect(Collectors.toList())));
        }
        List<SysButton> sysButtonList1 = new ArrayList<>();
        List<SysMenu> sysMenuList = sysMenuService.selectList(new QueryWrapper<SysMenu>());
        List<String> parenIds = sysMenuList.stream().map(SysMenu::getParentid).collect(Collectors.toList());
        sysMenuList = sysMenuList.stream().filter(sysMenu ->!parenIds.contains(sysMenu.getId())&&!sysMenu.getParentid().equals("0")).collect(Collectors.toList());
        sysMenuList.forEach(sysMenu -> {
            sysButtonList1.add(SysButton.builtSysButton(sysMenu,"添加","add","add"));
            sysButtonList1.add(SysButton.builtSysButton(sysMenu,"修改","edit","edit"));
            sysButtonList1.add(SysButton.builtSysButton(sysMenu,"查询","query","query"));
            sysButtonList1.add(SysButton.builtSysButton(sysMenu,"删除","delete","delete"));
            sysButtonList1.add(SysButton.builtSysButton(sysMenu,"导入","import","import"));
            sysButtonList1.add(SysButton.builtSysButton(sysMenu,"导出","export","export"));
        });
        return toAjax(sysButtonService.saveBatch(sysButtonList1));
    }

}

